/*
 * @lc app=leetcode.cn id=143 lang=javascript
 *
 * [143] 重排链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {void} Do not return anything, modify head in-place instead.
 */
/**
 * 将链表节点映射到数组中
 * 通过数字下标移动节点
 */
var reorderList = function(head) {
    if(!head || !head.next) return false
    let  p = head
    // 先打散链表，放入数组中  arr=[1,2,3,4]
    arr = []
    while(p){
        arr[arr.length] = p
        let temp = p.next;
        p.next = null;
        p = temp
    }
    // 重组链表
    let p1 = 0 //头部指针
    let p2 = arr.length - 1; //尾部指针
    let ret = new ListNode(null) 
    let p3 = ret //结果链表的尾指针
    while(p1 < p2){
        p3.next = arr[p1];
        p3 = p3.next;
        p3.next = arr[p2]
        p3 = p3.next

        p1++
        p2--
    }
    if(p1 === p2){
        p3.next = arr[p1]
    }
    return ret


};
// @lc code=end

